<?php
class MSEP_Encrypt extends MSEP_Object
{


      private $ISTHeaderNumber;



      private $image;



      public function __init(MSEP $core)
      {
            $this->core = $core;

            $this->ISTHeaderNumber = 8;
      }



      public function decrypt($filename)
      {
            $imageIn = fopen($filename, "rb");
            $this->headers[0] = 1;

            while (($char = fgetc($imageIn)) > -1)
            {
                  $charValue = hexdec(bin2hex($char));

                  if ($charValue == 0xff)
                  {
                        $char2 = fgetc($imageIn);
                        $charValue2 = hexdec(bin2hex($char2));

                        if ($charValue2 > 0xe0)
                        {
                              $length = hexdec(bin2hex(fgetc($imageIn).fgetc($imageIn)));

                              if (($charValue2 - 0xe0) == $this->ISTHeaderNumber)
                              {
                                    $this->headers[$charValue2 - 0xe0] = fread($imageIn,$length-2);
                                    fclose($imageIn);

                                    return $this->headers[$charValue2 - 0xe0];
                              }
                        }
                  }
            }
      }



      /*public function decrypt($filename)
      {
            if ($this->core->http()->http($filename, "contents"))
            {
                  $imageIn = $this->core->http()->contents;
                  $this->headers[0] = 1;

                  $j = strlen($imageIn);

                  $k = 0;

                  while (($char = substr($imageIn, $k, 1)) > -1 && $k < $j)
                  {
                        $charValue = hexdec(bin2hex($char));

                        if ($charValue == 0xff)
                        {
                              $char2 = substr($imageIn, ($k+1), 1);
                              $charValue2 = hexdec(bin2hex($char2));

                              if ($charValue2 > 0xe0)
                              {
                                    $length = hexdec(bin2hex(substr($imageIn, ($k+2), 1).substr($imageIn, ($k+3), 1)));

                                    if (($charValue2 - 0xe0) == $this->ISTHeaderNumber)
                                    {
                                          $this->headers[$charValue2 - 0xe0] = substr($imageIn, 0, $length-2);

                                          return $this->headers[$charValue2 - 0xe0];
                                    }
                              }
                        }

                        $k++;
                  }
            }
      }*/



      public function encrypt($filename, $text)
      {
            $imageIn = fopen($filename, "rb");

            while (($char = fgetc($imageIn)) > -1)
            {
                  $charValue = hexdec(bin2hex($char));
                  if ($charValue == 0xff)
                  {
                        $char2 = fgetc($imageIn);

                        if ($char2 < 0)
                        {
                              break;
                        }

                        $charValue2 = hexdec(bin2hex($char2));

                        if ($charValue2 == (0xe0 + $this->ISTHeaderNumber))
                        {
                              $length = hexdec(bin2hex(fgetc($imageIn).fgetc($imageIn)));

                              if ($length < 2)
                              {
                                    $length = 2;
                              }

                              fread($imageIn,$length-2);
                        }
                        elseif ($charValue2 > 0xe0 && $charValue2 < (0xe0 + $this->ISTHeaderNumber))
                        {
                              $lenA = fgetc($imageIn);
                              $lenB = fgetc($imageIn);
                              $newImage .= $char.$char2.$lenA.$lenB;
                              $length = hexdec(bin2hex($lenA.$lenB));

                              if ($length < 2)
                              {
                                    $length = 2;
                              }

                              $newImage .= fread($imageIn,$length-2);
                        }
                        else
                        {
                              if (($charValue2 < 0xe0 && $charValue2 != 0xd8) || $charValue2 > (0xe0 + $this->ISTHeaderNumber))
                              {
                                    if (strlen($text) > 0)
                                    {
                                          $length = str_pad(dechex(strlen($text) + 2),4,"0",STR_PAD_LEFT);
                                          $newImage .= pack("H*","FFe" . $this->ISTHeaderNumber . $length) . $text;
                                    }

                                    $newImage .= $char;
                                    $newImage .= $char2;

                                    break;
                              }

                              $newImage .= $char;
                              $newImage .= $char2;
                        }
                  }
                  else
                  {
                        $newImage .= $char;
                  }
            }

            $newImage .= @fread($imageIn, @filesize($filename));

            fclose($imageIn);

            if (!$filenameOut = @fopen($filename,'wb'))
            {
                  die("success=Error!\n\nCouldn't add annotation because \"" . $filename . "\" isn't writable!");
            }

            fwrite($filenameOut, $newImage);
            fclose($filenameOut);
      }


}
?>